﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;

namespace GieldaCSharp
{
    abstract class Border
    {
        public int maxXn = 70;
        public int minXn = 20;
        public int maxYn = 25;
        public int minYn = 5;
        public double liczbaPodzialek = 15;
    };

    class BorderAll : Border
    {
        public BorderAll() { }

        public BorderAll(PictureBox p)
        {
            maxXn = p.Size.Width - maxXn;
            maxYn = p.Size.Height - maxYn;
        } 
    };

    class Border2_3 : Border
    {
        public Border2_3() { }

        public Border2_3(PictureBox p)
        {
            maxXn = p.Size.Width - maxXn;
            maxYn = ((p.Size.Height/3) * 2) - maxYn;               
        }
    };

    class Border1_3 : Border
    {
        public Border1_3() { }

        public Border1_3(PictureBox p)
        {
            maxXn = p.Size.Width - maxXn;
            minYn = ((p.Size.Height / 3) * 2) - maxYn + 50;
            maxYn = p.Size.Height - maxYn;
        }
    };

    class Composite
    {
        //---------------------------Zmienne--------------------------------------------------
        PictureBox p;

        private Graphics g; // do narysowania wykresow na bmp
        private Graphics gPb; // do rysowania na picture boxie

        private Bitmap bmp;
        private Bitmap bmpTmp;

        private int N;

        protected int iOd;
        protected int iDo;

        private List<Component> components = new List<Component>(); // lista wykresow

        //---------------------------Metody---------------------------------------------------
        private Composite() { }

        public Composite(PictureBox _p)
        {
            p = _p;
            InitializeGraphics();
        }

        public void InitializeGraphics()
        {
            bmp = new Bitmap(p.Size.Width, p.Size.Height);
            bmpTmp = new Bitmap(p.Size.Width, p.Size.Height);

            g = Graphics.FromImage(bmp); //rysujemy na bmp
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        }

        public int UstalStart(List<kursV> lista, int OdK, int DoK)
        {
            bool start = false;
            bool stop = false;

            for (int i = 0; i < lista.Count; i++)
            {
                if ((lista[i].dataInt >= OdK) && (start == false))
                {
                    iOd = i;
                    start = true;
                }

                if ((lista[i].dataInt >= DoK))
                {
                    iDo = i;
                    stop = true;
                    break;
                }
            }
            if (false == stop) { iDo = lista.Count - 1; }

            N = iDo - iOd;

            return N;
        }

        public int getiOd() { return iOd; }
        
        public int getiDo() { return iDo; }

        public void AddComponent(Border _b, List<kursV> lista, int _size)
        {
            components.Add(new Component(p, lista, _b, g, N, iOd, iDo, _size));
        }

        public void AddComponent(Border _b)
        {
            components.Add(new Component(p, _b, g, N, iOd, iDo));
        }

        public void Add(Wykres w, int ind) //Dodaj wykres
        {            
            components[ind].wykresy.Add(w);
        }

        public void Clear() //Usun wykresy
        {
            components.Clear();
        }

        public void DrawBall(int ind) // Kropka poruszajaca sie po wykresie
        {
            if (0 == components.Count) return;

            //kropke rysujemy bezposrednio na picture box
            p.Image = bmpTmp;
            gPb = Graphics.FromImage(p.Image);
            gPb.Clear(Color.FromArgb(246, 246, 246));

            gPb.DrawImageUnscaled(bmp, 0, 0, bmp.Width, bmp.Height); //rysujemy wykres

            for (int i = 0; i < components.Count; i++)
            {
                components[i].DrawBall(ind, gPb);
            }

            gPb.Dispose(); //zwalniamy powierzchnię
        }

        public void DrawAll()
        {
            if (0 == components.Count) return;

            g.Clear(Color.FromArgb(246, 246, 246)); 

            for (int i = 0; i < components.Count; i++)
            {
                components[i].Draw();
            }

            //Przerysowuje bmp na picture box
            gPb = p.CreateGraphics(); //uzyskujemy powierzchnię
            gPb.DrawImageUnscaled(bmp, 0, 0, bmp.Width, bmp.Height); //rysujemy wykres na PictureBox
            gPb.Dispose(); //zwalniamy powierzchnię   
        }

        public void Refresh()
        {
            p.Image = bmpTmp;
            gPb = Graphics.FromImage(p.Image);
            gPb.Clear(Color.FromArgb(246,246,246));

            gPb.DrawImageUnscaled(bmp, 0, 0, bmp.Width, bmp.Height); //rysujemy wykres
            gPb.Dispose(); //zwalniamy powierzchnię           
        }

    }
}
